package com.example.mykotilin.viewpager2

import android.annotation.SuppressLint
import android.app.ActionBar
import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.widget.ViewPager2
import android.view.ViewGroup.LayoutParams
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.mykotilin.R

class ParallelNestedScrollingActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewpager = ViewPager2(this).apply {
            layoutParams = matchParent()
            orientation = ViewPager2.ORIENTATION_HORIZONTAL
            adapter = VpAdapter()
        }
        setContentView(viewpager)
    }

    //viewpager的适配器
    class VpAdapter : RecyclerView.Adapter<VpAdapter.ViewHolder>(){
        override fun getItemCount(): Int {
            return  4
        }
        @SuppressLint("ResourceType")
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val  inflater = LayoutInflater.from(parent.context)
            val root = inflater.inflate(R.layout.item_nested_recyclerviews,parent,false)

            return ViewHolder(root).apply {
                rv1.setUpRecyclerView(RecyclerView.HORIZONTAL)
                rv2.setUpRecyclerView(RecyclerView.VERTICAL)
            }
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            with(holder){
                title.text = title.context.getString(R.string.page_position, adapterPosition)
                itemView.setBackgroundResource(PAGE_COLORS[position % PAGE_COLORS.size])
            }
        }
        private fun  RecyclerView.setUpRecyclerView(orientation:Int){
            layoutManager = LinearLayoutManager(context,orientation,false)
            adapter = RvAdapter(orientation)
        }

        class ViewHolder(itemview: View) : RecyclerView.ViewHolder(itemview){
            val title: TextView = itemView.findViewById(R.id.page_title)
            val rv1: RecyclerView = itemView.findViewById(R.id.first_rv)
            val rv2: RecyclerView = itemView.findViewById(R.id.second_rv)
        }
    }

    class RvAdapter(private val orientation: Int) :RecyclerView.Adapter<RvAdapter.ViewHolder>(){
        override fun getItemCount(): Int {
            return 40
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val tv = TextView(parent.context)
            tv.layoutParams = matchParent().apply {
                if(orientation == RecyclerView.HORIZONTAL){
                    width = LayoutParams.WRAP_CONTENT
                }else{
                    height = LayoutParams.WRAP_CONTENT
                }
            }
            tv.textSize = 20f
            tv.gravity = Gravity.CENTER
            tv.setPadding(20,55,20,55)
            return ViewHolder(tv)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            with(holder) {
                tv.text = tv.context.getString(R.string.item_position, adapterPosition)
                tv.setBackgroundResource(CELL_COLORS[position % CELL_COLORS.size])
            }
        }


        class ViewHolder(val tv:TextView) :RecyclerView.ViewHolder(tv)
    }
}

internal fun matchParent(): LayoutParams {
    return LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
}

internal val PAGE_COLORS = listOf(
    R.color.yellow_300,
    R.color.green_300,
    R.color.teal_300,
    R.color.blue_300
)

internal val CELL_COLORS = listOf(
    R.color.grey_100,
    R.color.grey_300
)
